/*
 * @Description: 防盗链
 * @Author: wangqi
 * @Date: 2024-07-24 15:01:54
 * @LastEditTime: 2024-07-24 16:43:36
 * @LastEditAuthor: wangqi
 */

/* 
    防盗链总览
    - referer
        指定 referer 域名能看，其他域名禁止查看
        post 才有 referer; get 请求没有

*/
const fs = require('fs')
const path = require('path')
const express = require('express')
const logger = require("./_unit/logs")
const app = express()

app.use('/public', express.static(path.resolve(__dirname, './public/')))
const filePath = path.resolve(__dirname, './test/08_防盗链.html')

/* 防盗链中间件 */
function TheftChainMidWare(req, res, next) {
    let referer = req.get('referer');
    if (referer) {
        logger.info(referer)
        let { hostname } = new URL(referer)
        if (hostname !== '127.0.0.1') {
            res.status(404).send('<h2>找不到页面！</h2>')
        }
    }
    next()
}

app.use(TheftChainMidWare)

app.post("/qwe", (req, res) => {
    res.sendFile(filePath)
})

app.listen(3000)